package com.sqx.modules.errand.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.entity.UserMoney;
import com.sqx.modules.app.entity.UserMoneyDetails;
import com.sqx.modules.app.service.UserMoneyDetailsService;
import com.sqx.modules.app.service.UserMoneyService;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.errand.entity.*;
import com.sqx.modules.errand.service.AdminService;
import com.sqx.modules.errand.service.UserInfoService;
import com.sqx.modules.riderLine.entity.RiderLine;
import com.sqx.modules.riderLine.service.RiderLineService;
import com.sqx.modules.tbCoupon.entity.TbCouponUser;
import com.sqx.modules.tbCoupon.service.TbCouponUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

@RestController
@RequestMapping("/app/userinfo")
@Api(value = "用户端", tags = {"用户端"})
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private CommonInfoService commonInfoService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private UserMoneyService userMoneyService;
    @Autowired
    private TbCouponUserService couponUserService;
    @Autowired
    private UserService userService;
    @Autowired
    private RiderLineService riderLineService;

    @Login
    @GetMapping(value = "/findUserInfoById")
    @ApiOperation("获取登陆用户信息")
    public Result findUserInfoById(@RequestAttribute("userId") Long userId) {
        UserEntity userEntity = userInfoService.findUserInfoById(userId);
        if (userEntity != null) {
            UserMoney userMoney = userMoneyService.selectUserMoneyByUserId(userId);
            if (userMoney != null) {
                userEntity.setMoney(userMoney.getMoney());
            } else {
                userMoney = new UserMoney();
                userMoney.setUserId(userId);
                userMoney.setMoney(BigDecimal.ZERO);
                userMoneyService.save(userMoney);
            }
            userEntity.setCouponCount(couponUserService.count(new QueryWrapper<TbCouponUser>().eq("user_id", userId).eq("status", 0)));
            BigDecimal dayLine = riderLineService.getDayLine(userId);
            if (dayLine != null) {
                userEntity.setRiderLine(dayLine.divide(new BigDecimal(60), 2, RoundingMode.HALF_UP));
            }
        }
        return Result.success().put("data", userEntity);
    }


    @Login
    @ApiOperation("用户绑定手机")
    @PostMapping(value = "/checkPhone")
    public Result checkPhone(String openId, String phone, Integer userType) {

        return userInfoService.checkPhone(openId, phone, userType);
    }


    @Login
    @PostMapping(value = "updateUserInfoById")
    @ApiOperation("用户修改信息")
    public Result updateUserInfoById(@RequestAttribute Long userId, @RequestBody UserEntity userEntity) {
        userEntity.setUserId(userId);
        return userService.updateById(userEntity) ? Result.success() : Result.error();
    }

    @Login
    @GetMapping(value = "findAccount")
    @ApiOperation("获取用户账户明细")
    public Result findAccount(@RequestAttribute("userId") Long userId) {
        MoneyDetails moneyDetails = userInfoService.findAccount(userId);
        return Result.success().put("data", moneyDetails);
    }

    @Login
    @PostMapping(value = "userFeedback")
    @ApiOperation("用户反馈")
    public Result userFeedback(@RequestAttribute Long userId, @RequestBody Feedback feedback) {

        return userInfoService.userFeedback(userId, feedback);
    }


    @GetMapping(value = "userAgreement")
    @ApiOperation("用户协议")
    public Result userAgreement() {
        CommonInfo one = commonInfoService.findOne(245);
        return Result.success().put("data", one);
    }


    @GetMapping(value = "privacyPolicy")
    @ApiOperation("隐私政策")
    public Result privacyPolicy() {
        CommonInfo one = commonInfoService.findOne(246);
        return Result.success().put("data", one);
    }


    @ApiOperation("师傅代购服务用户协议")
    @GetMapping(value = "serviceAgreement")
    public Result serviceAgreement() {
        CommonInfo one = commonInfoService.findOne(256);
        return Result.success().put("data", one);
    }

    @Login
    @ApiOperation("师傅收入统计 完成订单数统计")
    @GetMapping(value = "findIncome")
    public Result findIncome(@RequestAttribute Long userId) {

        return userInfoService.findIncome(userId);
    }

    @Login
    @ApiOperation("获取师傅账户余额")
    @GetMapping(value = "findBalance")
    public Result findBalance(@RequestAttribute Long userId) {

        return userInfoService.findBalance(userId);
    }

    @Login
    @ApiOperation("获取师傅保证金")
    @GetMapping(value = "findCashDeposit")
    public Result findCashDeposit(@RequestAttribute Long userId) {

        return userInfoService.findCashDeposit(userId);
    }

    @Login
    @ApiOperation("获取师傅账户明细")
    @GetMapping(value = "findMoneyDetails")
    public Result findMoneyDetails(@RequestAttribute Long userId, Integer page, Integer limit, UserMoneyDetails userMoneyDetails) {
        userMoneyDetails.setUserId(userId);
        return Result.success().put("data", userInfoService.findMoneyDetails(userId, page, limit, userMoneyDetails));
    }

    @Login
    @ApiOperation("师傅查看用户订单评价")
    @GetMapping(value = "findEvaluate")
    public Result findEvaluate(@RequestAttribute Long userId, Integer page, Integer limit, String satisfactionFlag) {

        return userInfoService.findEvaluate(userId, page, limit, satisfactionFlag);
    }

    @Login
    @ApiOperation("新用户领取新人优惠券")
    @GetMapping(value = "newUserCoupon")
    public Result newUserCoupon(@RequestAttribute Long userId) {

        return userInfoService.newUserCoupon(userId);
    }

    @Login
    @ApiOperation("师傅改变上下线状态")
    @PostMapping(value = "updateOnLine")
    public Result updateOnLine(@RequestAttribute Long userId, Integer onLineFlag) {

        return userInfoService.updateOnLine(userId, onLineFlag);
    }

    @ApiOperation("接单排行榜")
    @GetMapping(value = "rankingList")
    public Result rankingList(Integer page, Integer limit, String address, String date, String dateType) {

        return userInfoService.rankingList(page, limit, address, date, dateType);
    }

    @ApiOperation("培训中心列表")
    @GetMapping(value = "trainingCenterList")
    public Result trainingCenterList(Integer page, Integer limit, String title, Integer type) {

        return adminService.adminTrainingCenter(page, limit, title, type);
    }

    @ApiOperation("培训中心详情")
    @GetMapping(value = "TrainingCenter")
    public Result TrainingCenter(Long id) {

        return adminService.TrainingCenter(id);
    }

    @Login
    @ApiOperation("判断用户是否被封号")
    @GetMapping(value = "bannedFlag")
    public Result bannedFlag(@RequestAttribute Long userId) {

        return userInfoService.bannedFlag(userId);
    }

    @Login
    @ApiOperation("用户保证金明细")
    @GetMapping(value = "selectCashDeposit")
    public Result selectCashDeposit(@RequestAttribute Long userId, Integer page, Integer limit) {

        return userInfoService.selectCashDeposit(userId, page, limit);
    }

}
